package com.bwda.dsrs.filecenter.service.impl;

import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.po.scanresultstatistics.ScanResultStatisticsPo;
import com.bwda.dsrs.filecenter.domain.condition.scanresultstatistics.ScanResultStatisticsCondition;
import com.bwda.dsrs.filecenter.domain.po.scanresulttable.NameCountPo;
import com.bwda.dsrs.filecenter.mapper.dsrs.ScanResultStatisticsMapper;
import com.bwda.dsrs.filecenter.service.ScanResultStatisticsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ScanResultStatisticsServiceImpl extends BaseServiceImpl<ScanResultStatisticsPo, ScanResultStatisticsCondition, ScanResultStatisticsMapper> implements ScanResultStatisticsService {

    @Override
    public List<NameCountPo> querySensWordNumList(Integer dbId) {
        List<NameCountPo> list = this.getMapper().querySensWordNumList(dbId);
        return list;
    }

    @Override
    public List<NameCountPo> querySensTypeNumList(Integer dbId) {
        List<NameCountPo> list = this.getMapper().querySensTypeNumList(dbId);
        return list;
    }

    @Override
    public List<NameCountPo> querySensLevelNumList(Integer dbId) {
        List<NameCountPo> list = this.getMapper().querySensLevelNumList(dbId);
        return list;
    }

    @Override
    public List<NameCountPo> statisticsTable(Integer dbId) {
        List<NameCountPo> list = this.getMapper().statisticsTable(dbId);
        return list;
    }

    @Override
    public List<NameCountPo> statisticsColumn(Integer dbId) {
        List<NameCountPo> list = this.getMapper().statisticsColumn(dbId);
        return list;
    }

    @Override
    public void statistics(Integer dbId) {
        List<NameCountPo> nameCountPosTable = this.statisticsTable(dbId);
        List<NameCountPo> nameCountPosColumn = this.statisticsColumn(dbId);
        if(!CollectionUtils.isEmpty(nameCountPosTable) && !CollectionUtils.isEmpty(nameCountPosColumn)){
            Map<Integer,NameCountPo> nameCountPosTableMap = nameCountPosTable.stream().collect(Collectors.toMap(NameCountPo::getDbId, Function.identity(),(key1, key2)->key1));
            for (NameCountPo nameCountPo:nameCountPosColumn
            ) {
                ScanResultStatisticsPo scanResultStatisticsPo = new ScanResultStatisticsPo();
//				DbInfoPo dbInfoPo = dbInfoService.query(nameCountPo.getDbId());
//				if(dbInfoPo!=null){
//					IRemoteService iRemoteService = SpringUtils.getBean(dbInfoPo.getDbType().concat("RemoteServiceImpl"));
//					try {
//						List<String> dbList =iRemoteService.getAllDBList(iRemoteService.login(dbInfoPo),dbInfoPo);
//						if(!CollectionUtils.isEmpty(dbList)){
//							scanResultStatisticsPo.setDbNum(dbList.size());
//						}
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
                scanResultStatisticsPo.setDbNum(1);
                scanResultStatisticsPo.setDbId(nameCountPo.getDbId());
                scanResultStatisticsPo.setTableNum(nameCountPosTableMap.get(nameCountPo.getDbId()).getSumNum());
                scanResultStatisticsPo.setSenstableNum(nameCountPosTableMap.get(nameCountPo.getDbId()).getSensNum());
                scanResultStatisticsPo.setColumnNum(nameCountPo.getSumNum());
                scanResultStatisticsPo.setSenscolumnNum(nameCountPo.getSensNum());
                scanResultStatisticsPo.setCreateTime(new Date());
                ScanResultStatisticsCondition condition = new ScanResultStatisticsCondition();
                condition.setDbId(nameCountPo.getDbId());
                List<ScanResultStatisticsPo> scanResultStatisticsPos = this.queryList(condition);
                if(!CollectionUtils.isEmpty(scanResultStatisticsPos)){
                    try {
                        this.delete(scanResultStatisticsPos.get(0).getDbId());
                    } catch (BusinessServiceException e) {
                        e.printStackTrace();
                    }
                }
                this.insert(scanResultStatisticsPo);
            }
        }
    }
}